package com.poisonh.unifiedauth.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.poisonh.unifiedauth.R;
import com.poisonh.unifiedauth.model.PassWdModel;
import com.poisonh.unifiedauth.utils.PinyinComparator;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * 密码箱
 * Created by PoisonH on 2016/7/21.
 */

public class RvPassWdAdapter extends RecyclerView.Adapter<RvPassWdAdapter.MyViewHolder>
{
    private Context mContext;
    private List<PassWdModel> mContactLists;

    private OnItemClickLitener mOnItemClickLitener;

    public void setOnItemClickLitener(OnItemClickLitener mOnItemClickLitener)
    {
        this.mOnItemClickLitener = mOnItemClickLitener;
    }

    public RvPassWdAdapter(Context context)
    {
        this.mContext = context.getApplicationContext();
        mContactLists = new ArrayList<>();
    }

    @Override
    public int getItemCount()
    {
        return mContactLists.size();
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType)
    {
        return new MyViewHolder(LayoutInflater.from(mContext).inflate(R.layout.layout_passwordbox_item, parent, false));
    }

    /**
     * 绑定数据集
     *
     * @param holder
     * @param position
     */
    @Override
    public void onBindViewHolder(final MyViewHolder holder, int position)
    {
        holder.mTvAccountName.setText(mContactLists.get(position).getNAME());
        holder.mTvAccount.setText(mContactLists.get(position).getACCOUNT());

        int section = getSectionForPosition(position);
        if (position == getPositionForSection(section))
        {
            holder.mTvCatagory.setVisibility(View.VISIBLE);
            holder.mTvCatagory.setText(mContactLists.get(position).getmStrFirstLetter());
        } else
        {
            holder.mTvCatagory.setVisibility(View.GONE);
        }
        /**
         * 分别设置点击事件的回调
         */
        if (mOnItemClickLitener != null)
        {
            holder.itemView.setOnClickListener(new View.OnClickListener()
            {
                @Override
                public void onClick(View v)
                {
                    int pos = holder.getLayoutPosition() - 1;
                    mOnItemClickLitener.onItemClick(v, pos);
                }
            });
        }

    }

    protected class MyViewHolder extends RecyclerView.ViewHolder
    {
        private TextView mTvCatagory;
        private TextView mTvAccountName;
        private TextView mTvAccount;


        protected MyViewHolder(View view)
        {
            super(view);

            //查找控件
            mTvCatagory = (TextView) view.findViewById(R.id.tv_catagory);
            mTvAccountName = (TextView) view.findViewById(R.id.tv_account_name);
            mTvAccount = (TextView) view.findViewById(R.id.tv_account);
        }
    }

    public void notif(List<PassWdModel> list)
    {
        this.mContactLists.addAll(mContactLists.size(), list);
        this.notifyDataSetChanged();
    }

    public void update(List<PassWdModel> list)
    {
        if (mContactLists.size() > 0)
        {
            mContactLists.clear();
        }
        this.mContactLists.addAll(list);
        this.notifyDataSetChanged();
    }


    public int getSectionForPosition(int position)
    {
        return mContactLists.get(position).getmStrFirstLetter().charAt(0);
    }

    public int getPositionForSection(int section)
    {
        for (int i = 0; i < getItemCount(); i++)
        {
            String sortStr = mContactLists.get(i).getmStrFirstLetter();
            char firstChar = sortStr.toUpperCase().charAt(0);
            if (firstChar == section)
            {
                return i;
            }
        }
        return -1;
    }

    public interface OnItemClickLitener
    {
        void onItemClick(View view, int position);
    }
}
